package org.fmblzf.library.asyc;

import android.os.Message;

import org.fmblzf.library.inter.InterListener;

/**
 * Created by Administrator on 2016/1/20.
 */
public class AsycThread implements Runnable {

    /**
     * 消息发送实体
     */
    private AsycHandler mAsycHandler;

    /**
     * 线程中具体的实现接口
     */
    private InterListener mListener;

    /**
     * 构造器
     * @param asycHandler
     */
    public AsycThread(AsycHandler asycHandler,InterListener interListener) {
        this.mAsycHandler = asycHandler;
        this.mListener = interListener;
    }

    @Override
    public void run() {
        //开始
        Message start = mAsycHandler.obtainMessage();
        start.what = Intant.START;
        mAsycHandler.sendMessage(start);

        //进行中
        Message process = mAsycHandler.obtainMessage();
        process.what = Intant.PROCESS;
        mAsycHandler.sendMessage(process);

        Object obj = null;
        if(mListener != null){
            try {
                obj = mListener.action(0,mAsycHandler);
            } catch (Exception e) {
                //失败
                Message fail = mAsycHandler.obtainMessage();
                fail.what = Intant.FAIL;
                fail.obj = e.getMessage();
                mAsycHandler.sendMessage(fail);
            }
        }
        //成功
        Message sucess = mAsycHandler.obtainMessage();
        sucess.what = Intant.SUCESS;
        sucess.obj = obj;
        mAsycHandler.sendMessage(sucess);

    }
}
